package com.yjt.gcss.gddw.modules.uiFileLog;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.integration.support.json.Jackson2JsonObjectMapper;
import org.springframework.integration.support.json.JacksonJsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjt.common.base.datasource.DBContextHolder;
import com.yjt.common.easyui.datagrid.DataGridDTO;
import com.yjt.common.easyui.datagrid.DataGridJsonDTO;
import com.yjt.common.model.JsonDTO;
import com.yjt.common.tools.Jackson2JsonUtil;
import com.yjt.gcss.gddw.common.bean.domain.TxnPlds;
import com.yjt.gcss.gddw.common.bean.domain.UiFileLog;
import com.yjt.gcss.gddw.common.bean.domain.UiPlds;
import com.yjt.gcss.gddw.common.bean.model.GDDWYJTRoutingMappingFileModel;
import com.yjt.gcss.gddw.modules.txnPlds.TxnPldsService;
import com.yjt.gcss.gddw.modules.uiPlds.UiPldsService;
import com.yjt.yjtrouting.common.bean.domain.InfoTxnLogMapping;
import com.yjt.yjtrouting.modules.infoTxnLogMapping.InfoTxnLogMappingService;

@Controller
@RequestMapping("/gcss/gddw/UiFileLogController")
public class UiFileLogController {

	@Resource(name = UiFileLogService.BEAN_PACKAGE_NAME)
	private UiFileLogService uiFileLogService;
	
	@Resource(name = InfoTxnLogMappingService.BEAN_PACKAGE_NAME)
	private InfoTxnLogMappingService infoTxnLogMappingService;
	
	@Resource(name = UiPldsService.BEAN_PACKAGE_NAME)
	private UiPldsService uiPldsService;
	
	@Resource(name = TxnPldsService.BEAN_PACKAGE_NAME)
	private TxnPldsService txnPldsService;
	
	@RequestMapping("/index")
	public String index() {
		return "/WEB-INF/admin/gcss/gddw/uiFileLog/index";
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public DataGridJsonDTO query(UiFileLog queryModel, HttpServletRequest request) {
		DataGridJsonDTO result = new DataGridJsonDTO();
		List<GDDWYJTRoutingMappingFileModel> rows = new ArrayList<GDDWYJTRoutingMappingFileModel>();
		//设置电网数据库源
		DBContextHolder.setDBType(DBContextHolder.GDDW_SESSION_FACTORY);
		DataGridDTO _dataGridDto = new DataGridDTO(request);
		DataGridJsonDTO uiFileLogDataGridJsonDTO = uiFileLogService.query(queryModel, _dataGridDto);
		Collection<UiFileLog> uiFileLogCol = uiFileLogDataGridJsonDTO.getRows();
		DBContextHolder.setDBType(DBContextHolder.YJTROUTING_SESSION_FACTORY);
		String[] requestMsgIds = new String[uiFileLogCol.size()];
		if(uiFileLogCol != null) {
			int i = 0;
			for(UiFileLog _uiFileLog : uiFileLogCol) {
				GDDWYJTRoutingMappingFileModel _model = new GDDWYJTRoutingMappingFileModel();
				_model.setUiFileLog(_uiFileLog);
				requestMsgIds[i] = _uiFileLog.getFileId();
				rows.add(_model);
				i++;
			}
		}
		if(requestMsgIds.length != 0) {
			Collection<InfoTxnLogMapping> infoTxnLogMappingCol = infoTxnLogMappingService.getInfoTxnLogMappingColByReqMsgIds(requestMsgIds);
			if(infoTxnLogMappingCol != null) {
				for(InfoTxnLogMapping _infoTxnLogMapping : infoTxnLogMappingCol) {
					for(GDDWYJTRoutingMappingFileModel _model : rows) {
						if(_model.getUiFileLog().getFileId().equals(_infoTxnLogMapping.getRequestMsgId())) {
							_model.setInfoTxnLogMapping(_infoTxnLogMapping);
						}
					}
				}
			}
		}
		result.setRows(rows);
		result.setTotal(uiFileLogDataGridJsonDTO.getTotal());
		return result;
	}
	
	@RequestMapping("/viewDetailForward")
	public String viewDetailForward(@RequestParam(required = true)String fileId, HttpServletRequest request) {
		request.setAttribute("fileId", fileId);
		return "/WEB-INF/admin/gcss/gddw/uiFileLog/viewDetail";
	}
	
	@RequestMapping("/queryDetail")
	@ResponseBody
	public DataGridJsonDTO queryDetail(UiPlds queryModel, HttpServletRequest request) {
		DBContextHolder.setDBType(DBContextHolder.GDDW_SESSION_FACTORY);
		return uiPldsService.query(new DataGridDTO(request), queryModel);
	}
	
	@RequestMapping("/viewDetail")
	@ResponseBody
	public JsonDTO viewDetail(@RequestParam(required = true)String msgId, HttpServletRequest request) {
		UiPlds uiPldsQueryModel = new UiPlds();
		uiPldsQueryModel.setMsgId(msgId);
		DBContextHolder.setDBType(DBContextHolder.GDDW_SESSION_FACTORY);
		uiPldsQueryModel = uiPldsService.getUiPldsByQueryModel(uiPldsQueryModel);
		TxnPlds txnPldsQueryModel = new TxnPlds();
		txnPldsQueryModel.setReqMsgId(msgId);
		DBContextHolder.setDBType(DBContextHolder.GDDW_SESSION_FACTORY);
		txnPldsQueryModel = txnPldsService.getTxnPldsByQueryModel(txnPldsQueryModel);
		JsonDTO retJson = new JsonDTO();
		retJson.setSuccess(true);
		try {
			String result = "[";
			//这个顺序跟JS解析顺序一致
			result += Jackson2JsonUtil.beanToJson(uiPldsQueryModel) + ",";
			result += Jackson2JsonUtil.beanToJson(txnPldsQueryModel);
			result += "]";
			retJson.setObj(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retJson;
	}
}
